<?php

namespace app\backend\controller;

use app\backend\library\DataController;
use app\backend\validator\UpdOrderValidator;
use app\common\model\Order as OrderModel;

class Order extends DataController
{
    protected $mdl = OrderModel::class;

    protected $allowMainFields = ['id', 'order_num', 'status', 'state', 'goods_name', 'goods_price', 'goods_amount', 'payment_price', 'payment_type', 'account', 'payment_details', 'payment_time','remark', 'created_at', 'transaction_id', 'owner_address'];

    protected $allowSortFields = [
        "id",
        "status",
        "state",
        "created_at",
        "payment_time",
        'payment_type',
        'goods_amount',
        'goods_price',
        'payment_price'
    ];

    protected $allowSearchFields = [
        'id',
        "account",
        "goods_name",
        "order_num",
        'status',
        'state',
        'created_at',
        'payment_type',
        'payment_time',
        'transaction_id',
        'owner_address',
    ];

    protected function applySearch($query, $params)
    {
        parent::applySearch($query, $params);

        $query->where($this->mainFieldAlias('is_deleted'), $this->mdl::NO_DELETED);
    }

    protected function _printDataHook(&$items)
    {
        foreach ($items as $item) {
            if(!empty($item->goods_price)) {
                $item->goods_price = round($item->goods_price / 100, 2);
            }

            if(!empty($item->goods_name)) {
                $item->goods_name = base64_decode($item->goods_name);
            }
        }
    }

    protected function _saveBeforeHook(&$params)
    {
        $this->validate($params, UpdOrderValidator::class);
    }

}